import Vue from 'vue'
import VueRouter from 'vue-router'
import IndexView from '@/views/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: IndexView,
    meta: {
      showNavBar: true, showFooterBar: true,
      title: '',
      content: {
        keywords: '',
        description: ''
      }
    },
    beforeEnter: (to, from, next) => { next(); }
  },
  {
    path: '/index',
    name: 'IndexShou',
    component: IndexView,
    meta: {
      showNavBar: true, showFooterBar: true,
      title: '',
      content: {
        keywords: '',
        description: ''
      }
    },
    beforeEnter: (to, from, next) => { next(); }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register'),
    meta: {
      showNavBar: false, showFooterBar: false,
      title: '',
      content: {
        keywords: '',
        description: ''
      }
    },
    beforeEnter: (to, from, next) => { next(); }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login'),
    meta: { showNavBar: false, showFooterBar: false },
    beforeEnter: (to, from, next) => { next(); }
  },
  {
    path: '/password',
    name: 'Password',
    component: () => import('@/views/password'),
    meta: { showNavBar: false, showFooterBar: false },
    beforeEnter: (to, from, next) => { next(); }
  },

  //关于我们 
  {
    path: '/about/about',
    name: 'About',
    component: () => import('@/views/about/about'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); }
  },

  // 产品服务
  {
    path: '/serives/serives',
    name: 'Serives',
    component: () => import('@/views/serives/serives'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); }
  },

  // 实时新讯
  {
    path: '/news/news',
    name: 'News',
    component: () => import('@/views/news/news'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); }
  },

  // 百家论坛
  {
    path: '/froum/froum',
    name: 'Froum',
    component: () => import('@/views/froum/froum'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); }
  },

  // 百家姓
  {
    path: '/family/family',
    name: 'Family',
    component: () => import('@/views/family/family'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); },
  },

  // 百家姓详情列表
  {
    path: '/family/search/searchPage',
    name: 'SearchPage',
    component: () => import("@/views/family/search/searchPage.vue"),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next() }
  },

  // 会员中心
  {
    path: '/center/center',
    name: 'Center',
    component: () => import('@/views/center/center'),
    meta: { showNavBar: true, showFooterBar: true },
    beforeEnter: (to, from, next) => { next(); },
  },

  // 账号管理
  {
    path:'/management',
    name:'Management',
    component:()=>import('@/views/center/management.vue'),
    beforeEnter:(to,from,next) => { next();},
  }
]

// 解决点击路由重复报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes,
})

// 添加 SEO META 标签
router.beforeEach((to, from, next) => {
  if (to.meta.content) {
    const head = document.getElementsByTagName('head')[0];
    // 删除现有名称为 "keywords" 和 "description" 的 meta 标签
    const existingKeywordsMeta = document.querySelector('meta[name="keywords"]');
    if (existingKeywordsMeta) {
      existingKeywordsMeta.remove(); // 移除现有的关键词 meta 标签
    }
    const existingDescriptionMeta = document.querySelector('meta[name="description"]');
    if (existingDescriptionMeta) {
      existingDescriptionMeta.remove(); // 移除现有的描述 meta 标签
    }

    // 创建并设置新的关键词和描述 meta 标签
    const keywordsMeta = document.createElement('meta');
    keywordsMeta.setAttribute('name', 'keywords');
    keywordsMeta.setAttribute('content', to.meta.content.keywords);
    head.appendChild(keywordsMeta); // 添加新的关键词 meta 标签

    const descriptionMeta = document.createElement('meta');
    descriptionMeta.setAttribute('name', 'description');
    descriptionMeta.setAttribute('content', to.meta.content.description);
    head.appendChild(descriptionMeta); // 添加新的描述 meta 标签

    // 可选：如果需要，更新名为 "content" 的其他 meta 标签
    const contentMeta = document.querySelector('meta[name="content"]');
    if (contentMeta) {
      contentMeta.setAttribute('content', to.meta.content); // 更新 content 属性
    }
  }
  next();
});

export default router
